package com.fy.tt.service.impl;

import com.fy.tt.action.TemplateTool;
import com.fy.tt.constant.BLSConstant;
import com.fy.tt.dao.WordDao;
import com.fy.tt.entity.vo.WordAndSemanticVo;
import com.fy.tt.service.ParserRelatedFile;
import com.fy.tt.util.GenerateJar;
import com.fy.tt.util.GenerateParser;

import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ParserRelatedFileImpl implements ParserRelatedFile {
    WordDao wordDao = null;
    Connection dbConnection = null;

    {
        try {
            wordDao = TemplateTool.getBean(WordDao.class);
            dbConnection = TemplateTool.getDBConnection();
        } catch (Exception e) {

        }
    }

    public void generateG4File(String outPutPath) throws IOException {
        List<WordAndSemanticVo> wordSemantic = getWordAndSemantic();
        // g4文件固定部分 + Accept: '吸收' | '接纳' ;
        String part_01 = "";
        // noun: Noun_1 | Noun_2;
        String part_02 = "";
        // verb: Verb_1 | NVerb_2;
        String part_03 = "";
        StringBuffer sb = new StringBuffer();
        for (WordAndSemanticVo item : wordSemantic) {
            if (item.getName().equals("com.language")) {
                sb.append(item.getCode() + "\n");
                continue;
            }
            if (item.getName().contains(";")) {
                String[] names = item.getName().split(";");
                sb.append(item.getClassName()).append(": ");
                for (String name : names) {
                    sb.append("'" + name + "' | ");
                }
                sb.delete(sb.lastIndexOf("|"), sb.length()).append(";\n").toString();
            } else {
                sb.append(item.getClassName()).append(": '" + item.getName() + "';\n");
            }
        }
        part_01 = sb.toString();
        System.out.println("part_01 = " + part_01);

        StringBuffer sbNoun = new StringBuffer();
        sbNoun.append("noun: ");
        for (WordAndSemanticVo item : wordSemantic) {
            if (item.getType() == BLSConstant.WORD_TYPE_NOUN.intValue()) {
                sbNoun.append(item.getClassName() + " | ");
            }
        }
        sbNoun.delete(sbNoun.lastIndexOf("|"), sbNoun.length()).append(";\n").toString();
        part_02 = sbNoun.toString();
        System.out.println("part_02 = " + part_02);

        StringBuffer sbVerb = new StringBuffer();
        sbVerb.append("verb: ");
        for (WordAndSemanticVo item : wordSemantic) {
            if (item.getType() == BLSConstant.WORD_TYPE_VERB.intValue()) {
                sbVerb.append(item.getClassName() + " | ");
            }
        }
        sbVerb.delete(sbVerb.lastIndexOf("|"), sbVerb.length()).append(";\n").toString();
        part_03 = sbVerb.toString();
        System.out.println("part_03 = " + part_03);
        String content = part_01 + part_02 + part_03;

        FileOutputStream io = new FileOutputStream(outPutPath);
        BufferedOutputStream bos = new BufferedOutputStream(io);
        bos.write(content.getBytes(), 0, content.getBytes().length);
        bos.flush();
        bos.close();
    }

    public void generateClaFile(String outPutPath) throws IOException {
        List<WordAndSemanticVo> wordSemantic = getWordAndSemantic();
        // 部门 com.language.node.word.noun.Department;
        String part_04 = "";
        StringBuffer cla = new StringBuffer();
        for (WordAndSemanticVo item : wordSemantic) {
            if (item.getType() == BLSConstant.WORD_TYPE_NOUN.intValue()) {
                if (item.getName().contains(";")) {
                    String[] names = item.getName().split(";");
                    for (String name : names) {
                        cla.append(name + " com.language.node.word.noun." + item.getClassName() + "\n");
                    }
                } else {
                    cla.append(item.getName() + " com.language.node.word.noun." + item.getClassName() + "\n");
                }

            }

            if (item.getType() == BLSConstant.WORD_TYPE_VERB.intValue()) {
                if (item.getName().contains(";")) {
                    String[] names = item.getName().split(";");
                    for (String name : names) {
                        cla.append(name + " com.language.node.word.verb." + item.getClassName() + "\n");
                    }
                } else {
                    cla.append(item.getName() + " com.language.node.word.verb." + item.getClassName() + "\n");
                }
            }
        }
        part_04 = cla.toString();
        FileOutputStream io = new FileOutputStream(outPutPath);
        BufferedOutputStream bos = new BufferedOutputStream(io);
        bos.write(part_04.getBytes(), 0, part_04.getBytes().length);
        bos.flush();
        bos.close();
    }

    List<WordAndSemanticVo> getWordAndSemantic() {
        List<WordAndSemanticVo> wordSemantic = null;
        try {
            wordSemantic = wordDao.getWordSemantic(dbConnection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (wordSemantic == null) {
            throw new RuntimeException("word、semantic表获取失败");
        }
        return wordSemantic;
    }

    @Override
    public void updateExecutor(String outPutPath) throws IOException {
        System.out.println("------- 开始更新Executor  -------");
        System.out.println("------- 读取数据库生成g4文件  -------");
        generateG4File(outPutPath);
        System.out.println("------- 读取数据库生成cla文件  -------");
        generateClaFile(outPutPath);
        System.out.println("------- 根据g4文件生成部分文件  -------");
        GenerateParser.generatePaser();
        System.out.println("------- 编译代码生成Executor  -------");
//        GenerateJar.replaceLanguagePackagePath("G:\\test3\\languageExecutor\\src\\com\\language\\node\\word1","1");
//        GenerateJar.replaceLanguagePackagePath("G:\\test3\\languageExecutor\\src\\com\\language\\node\\word5","5");
        GenerateJar.generateJar();
        System.out.println("------- Executor更新完成  -------");
    }

}
